<?php

namespace Admin\Controller;

use Think\Controller;

/**
 * 模型管理
 */
class AdvController extends CommonController {

    var $formatTree;

    public function index() {

        $list = $this->lists("AdvPosition",['status'=>['egt',0]]);
        $this->assign("_list", $list);
        $this->meta_title = '广告位列表';
Cookie('__forward__', $_SERVER['REQUEST_URI']);
        $this->display();
    }

    /**
     * 文章添加
     */
    public function add() {
        $advPosition = D("AdvPosition");
        if (IS_POST) {
            if ($advPosition->create()) {
                if ($art_id = $advPosition->add()) {
                    $this->success("添加成功", Cookie('__forward__'));
                } else {
                    $this->error("添加失败");
                }
            } else {
                $this->error($advPosition->getError());
            }
        }
        $this->meta_title = '新增广告位';
        $this->display();
    }

    /**
     * 分类编辑
     */
    public function edit() {
        $id = I("id");
        if (IS_POST) {
             $advPosition = D("AdvPosition");
            if ($advPosition->create()) {
                if ($advPosition->save()) {
                    $this->success("编辑成功", Cookie('__forward__'));
                } else {
                    $this->error("编辑失败");
                }
            }
        }
        $ap_id=I("ap_id");
        $info=D("AdvPosition")->where(array("ap_id"=>$ap_id))->find();
        $this->assign("info",$info);
        $this->meta_title = '编辑广告位';
        $this->display();
    }

    public function sysmanage($ap_id) {
        $list = $this->lists("Adv",array("status"=>array("egt",0)));
        $this->assign("_list", $list);

        $this->assign("ap_id", $ap_id);
        Cookie('__forward__', $_SERVER['REQUEST_URI']);
        $this->meta_title = '广告列表';
        return $this->display();
    }

    public function adv_add() {
        $ap_id = I("ap_id");
        if (IS_POST) {
             $adv=D("Adv");
       
           if ($adv->create()) {
                if ($adv->add()) {
                    $this->success("编辑成功", Cookie('__forward__'));
                } else {
                    $this->error("编辑失败");
                }
            }else{
                $this->error($adv->getError());
            } 
        }
        //获取广告位属性
        $ap_info = D("AdvPosition")->where("ap_id", $ap_id)->find();
        $this->assign("ap_info", $ap_info);     //广告位消息
        $this->meta_title = '添加广告';
        $this->display();
    }

    public function adv_edit() {
        $adv_id = I("adv_id");
        if (IS_POST) {
            $adv=D("Adv");
                  $is_show=I("is_show");
             if(empty($is_show)){
                 $_POST["is_show"]=0;
             }
           if ($adv->create()) {
                if ($adv->save()) {
                    $this->success("编辑成功", Cookie('__forward__'));
                } else {
                    $this->error("编辑失败");
                }
            }else{
                $this->error($adv->getError());
            } 
        }
        //获取广告位属性
        $info = D("Adv")->where(array("adv_id"=> $adv_id))->find();
        $this->assign("info", $info);
        $ap_info = D("AdvPosition")->where("ap_id", $info['ap_id'])->find();
        $this->assign("ap_info", $ap_info);     //广告位消息
        $this->meta_title = '编辑广告';
        $this->display();
    }
    /**
    * 修改状态
    */
   public function changeStatus(){
        $method = I("method");
        $id = I("id", 0);
         $status = I("status");
        if ($status == 1) {
            $status = 0;
        } else {
            $status = 1;
        }
       switch ($method){
           case 'deleteAdvPosition':
                $this->delete("AdvPosition", array("ap_id" => $id));
                break;
           case 'deleteArticle':
                  if(D("Adv")->where(array("adv_id"=>$id))->delete()){
                      $this->success("删除成功");
                  }else{
                      $this->error("删除失败");
                  }
                 //$this->delete("AdvPosition", array("ap_id" => $id));
               break;
       }
   }
}
